import { route } from "quasar/wrappers";
import {
  createRouter,
  createMemoryHistory,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import routes from "./routes";
import { useUserStore } from "src/stores/modules/userStore";
import { LocalStorage } from "quasar";

export default route(function (/* { store, ssrContext } */) {
  const createHistory = process.env.SERVER
    ? createMemoryHistory
    : process.env.VUE_ROUTER_MODE === "history"
    ? createWebHistory
    : createWebHashHistory;

  const Router = createRouter({
    scrollBehavior: () => ({ left: 0, top: 0 }),
    routes,
    history: createHistory(
      process.env.MODE === "ssr" ? void 0 : process.env.VUE_ROUTER_BASE
    ),
  });

  Router.beforeEach((to, from, next) => {
    const loginStatus = LocalStorage.getItem("loginStatus");
    if (to.name === "login") {
      // Allow navigation to the login page
      next();
    } else {
      // Redirect to the login page if not logged in
      if (!loginStatus) {
        next({ name: "login" });
      } else {
        next();
      }
    }
  });

  // Add a navigation guard to redirect the user to the home page
  // Router.beforeEach((to, from, next) => {
  //   if (from.path === '/page1' || from.path === '/page2' || from.path === '/page3') {
  //     // Redirect the user to the home page
  //     next('/')
  //   } else {
  //     // Allow the user to continue navigating as usual
  //     next()
  //   }
  // })

  // Router.beforeEach(async (to) => {
  //   // redirect to login page if not logged in and trying to access a restricted page
  //   const publicPages = ['/auth']
  //   const authRequired = !publicPages.includes(to.path)
  //   const userStore = useUserStore()

  //   if (authRequired && !userStore.loginStatus) {
  //     userStore.returnUrl = to.fullPath
  //     return '/auth'
  //   }
  // })

  return Router;
});
